<template>
  <div class="app-container leaguer">
    <!-- 工具栏 start-->
    <EzForm
      ref="menuForm"
      :formMap="formMap"
      v-model="formData"
      @search="searchRequest"
    ></EzForm>
    <BtnGroup ref="btnGroup" :btns="btns" @handle="handleBtnGroup" />

    <!-- 工具栏 end-->

    <div class="">
      <el-table
        ref="table"
        :data="lists"
        :height="st"
        v-loading="loading"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        row-key="id"
        lazy
        :tree-props="{ hasChildren: 'subOrder' }"
      >
        <el-table-column type="selection" width="55"></el-table-column>

        <el-table-column label="版本名称" align="center" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.version }}</span>
          </template>
        </el-table-column>
        <el-table-column label="版本号" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.version_no }}</span>
          </template>
        </el-table-column>

        <el-table-column label="版本包下载链接" width="150" align="center">
          <template slot-scope="scope">
            <span><a :href="scope.row.version_url" target="_blank">{{ scope.row.version_url }}</a></span>
          </template>
        </el-table-column>
        <el-table-column label="完整版本版本号" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.full_version_no }}</span>
          </template>
        </el-table-column>

        <el-table-column label="完整版本版本包下载链接" width="150" align="center">
          <template slot-scope="scope">
            <span><a :href="scope.row.full_version_url" target="_blank">{{ scope.row.full_version_url }}</a></span>
          </template>
        </el-table-column>

        <el-table-column label="版本发布时间" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.release_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="版本说明" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="scope.$index === 0"
              type="text"
              size="mini"
              @click="handleAction(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :page="currentPage"
        :total="total"
        :size="pageSize"
        @sizeChange="sizeChange"
        @currentChange="currentChange"
        ref="paginationTotal"
      ></pagination>
    </div>

    <!--新建弹框-->
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      :before-close="modalClose"
      :close-on-press-escape="false"
      append-to-body
    >
      <div v-loading="loading">
        <el-form
          :model="actionForm"
          ref="actionForm"
          label-width="120px"
          size="small"
          :rules="rules"
        >
          <el-form-item label="版本名称：" prop="version">
            <el-input
              v-model.trim="actionForm.version"
              placeholder="请输入版本名称："
              maxlength="50"
              @change="rowChange"
            />
          </el-form-item>
          <el-form-item label="版本号：" prop="version_no">
            <el-input
              v-model.trim="actionForm.version_no"
              placeholder="请输入版本号："
              maxlength="10"
              @change="rowChange"
            />
          </el-form-item>
          <el-form-item label="版本包下载链接：" prop="version_url">
            <el-input
              v-model.trim="actionForm.version_url"
              placeholder="请输入版本包下载链接："
              maxlength="125"
              @change="rowChange"
            />
          </el-form-item>
          <el-form-item label="完整版本版本号：" prop="full_version_no">
            <el-input
              v-model.trim="actionForm.full_version_no"
              placeholder="请输入完整版本版本号："
              maxlength="10"
              @change="rowChange"
            />
          </el-form-item>
          <el-form-item label="完整版本版本包下载链接：" prop="full_version_url">
            <el-input
              v-model.trim="actionForm.full_version_url"
              placeholder="请输入完整版本版本包下载链接："
              maxlength="125"
              @change="rowChange"
            />
          </el-form-item>
          <el-form-item label="发布日期：" prop="release_at">
            <el-date-picker
              v-model.trim="actionForm.release_at"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="版本说明：" prop="remark">
            <el-input
              v-model.trim="actionForm.remark"
              placeholder="请输入版本说明："
              maxlength="10"
              @change="rowChange"
            />
          </el-form-item>

        </el-form>

        <div class="tool2">
          <el-button @click="handleSubmit" type="primary" size="medium"
            >确定</el-button
          >
          <el-button @click="modalClose" size="medium">取消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formMap: [
        {
          label: "版本名称",
          key: "version",
          type: "input",
        },
        {
          label: "版本号",
          key: "version_no",
          type: "input",
        },
        {
          label: "完整版本版本号",
          key: "full_version_no",
          type: "input",
        },
        {
          label: "版本发布时间",
          key: "release_at",
          type: "releasedate",
        },
      ],
      formData: {
        version: "",
        version_no: "",
        full_version_no: "",
        release_at: [],
      },
      btns: [
        { title: "新增", pos: "right", handle: "new" },
       
      ],
      lists: [],
      loading: true,
      dialogExamine: false,
      examineList: "",
      examineListLength: 0,
      // 默认显示第几页
      currentPage: 1,
      // 总条数，根据接口获取数据长度(注意：这里不能为空)
      total: 0,
      // 个数选择器（可修改）
      pageSizes: [50, 100, 200],
      // 默认每页显示的条数（可修改）
      pageSize: 30,

      st: 0,
      dialogVisible: false,
      dialogTitle: "创建版本",

      dict_list: {
      },

      actionForm: {
        version: "",
        version_no: "",
        version_url: "",
        full_version_no: "",
        full_version_url: "",
        release_at: "",
        remark: "",
      },

      created_at: [],

      dialogloading: true,
      classify_name: "",

      synchroUserLodaing: false,
      addFrom: {
        phone: "",
        name: "",
      },
      // 验证表单
      rules: {
          // 版本名称
        version: [{ required: true, message: "请输入版本名称", trigger: "blur" }],
          // 版本号
        version_no: [{ required: true, message: "请输入版本号", trigger: "blur" }],
          // 版本下载地址
        version_url: [{ required: true, message: "请输入版本下载地址", trigger: "blur" }],
          // 完整版版本号
        full_version_no: [{ required: true, message: "请输入完整版版本号", trigger: "blur" }],
          // 完整版版本下载地址
        full_version_url: [{ required: true, message: "请输入完整版版本下载地址", trigger: "blur" }],
          // 发布时间
        release_at: [{ required: true, message: "请输入发布时间", trigger: "blur" }],
          // 版本说明
        remark: [{ required: true, message: "请输入版本说明", trigger: "blur" }],
      },
      chooseList: [], //选中处理的列表
      roleDict: [], //角色
    };
  },
  created() {
    this.handleInit();
  },

  mounted() {
    let that = this;
    that.$util.autoHeight(that, that.$refs.table.$el);
  },
  methods: {
    /**
     * 头部按钮列表
     */
    handleBtnGroup(evt) {
      let { handle } = evt;
      switch (handle) {
        case "new":
          this.dialogVisible = true;
          break;
        default:
          break;
      }
    },

    // 关闭对话框
    modalClose() {
      this.dialogVisible = false;
      this.dialogExamine = false;
    },

    // 新增，编辑
    async handleAction(row = null) {
      let that = this;

      if (row != null) {
        //编辑回显
        that.dialogTitle = "编辑版本";

        that.actionForm.id = row.id;
        that.actionForm.version = row.version;
        that.actionForm.version_no = row.version_no;
        that.actionForm.version_url = row.version_url;
        that.actionForm.full_version_no = row.full_version_no;
        that.actionForm.full_version_url = row.full_version_url;
        that.actionForm.release_at = row.release_at;
        that.actionForm.remark = row.remark;
      } else {
        that.dialogTitle = "创建版本";

        that.actionForm.id = "";
        that.actionForm.version = "";
        that.actionForm.version_no = "";
        that.actionForm.version_url = "";
        that.actionForm.full_version_no = "";
        that.actionForm.full_version_url = "";
        that.actionForm.release_at = "";
        that.actionForm.remark = "";
      }

      that.dialogloading = false;
      that.dialogVisible = true;
    },

    // 提交
    async handleSubmit(e) {
      let that = this;
      that.$refs.actionForm.validate(async (v) => {
        if (!v) {
          return false;
        }
        try {
          that.dialogloading = true;
          let { result } = await that.$.setting.versionAction(
            that.actionForm
          );
          that.dialogloading = false;
          that.dialogVisible = false;
          that.handleSearch();
        } catch (e) {
          that.dialogloading = false;
        }
      });
    },

    // 搜索
    handleSearch(statu) {
      this.currentPage = 1;
      this.handleRequest();
    },

    // 弹框的行更改
    rowChange(e) {
      console.log(e);
    },

    handleSizeChange(e) {
      console.log(e);
    },
    handleCurrentChange(e) {
      console.log(e);
    },
    searchRequest() {
      this.currentPage = 1;
      this.handleRequest();
    },
    sizeChange(e) {
      let that = this;
      that.currentPage = 1;
      that.pageSize = e;
      that.handleRequest();
    },
    currentChange(e) {
      this.currentPage = e;
      this.handleRequest();
    },

    //请求列表
    async handleRequest(statu) {
      const that = this;
      let { currentPage, pageSize, created_at, department_id, formData } = that;
      let o = { page_size: pageSize, page: currentPage };
      o.start_release_at = !!formData.release_at ? formData.release_at[0] : "";
      o.end_release_at = !!formData.release_at ? formData.release_at[1] : "";
      that.$util.ext(that.formData, o);

      if (department_id) {
        that.formData.department_id = department_id[department_id.length - 1];
      }

      try {
        that.loading = true;
        let { result } = await that.$.setting.version(that.formData);
        that.total = result.pagination.total_count;
        if (!statu) {
          that.$refs.table.bodyWrapper.scrollTop = 0;
        }
        that.lists = result.list;
        that.loading = false;
      } catch (e) {
        that.loading = false;
      }
    },

    //初始化
    async handleInit() {
      const that = this;
      await this.handleRequest();
    },

    handleSelectionChange(e) {
      console.log(e);
    },
  },
};
</script>
